package com.sdusoft.action;

import java.io.FileInputStream;
import java.io.IOException;

import my.mvc.Annotation;
import my.mvc.RequestContext;
import my.util.Multimedia;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import com.sdusoft.beans.File;
import com.sdusoft.beans.User;
import com.sdusoft.service.StorageService;
import com.sdusoft.toolbox.LinkTool;

/**
 * 文件库
 * @author Winter Lau
 * @date 2010-7-4 下午01:35:12
 */
public class FileAction {
		
	public final static long MAX_FILE_SIZE = 2 * 1024 * 1024;

	/**
	 * 文件下载 /action/file/download?id=7&fn=fsfsd
	 * fn即ident
	 * @param ctx
	 * @throws IOException 
	 */
	public void download(RequestContext ctx) throws IOException {		
		if(ctx.isRobot()){
			ctx.forbidden();
			return ;
		}
		
		long id = ctx.id();
		File bean = File.INSTANCE.Get(id);
		if(bean == null){
			ctx.not_found();
			return ;
		}

		String f_ident = ctx.param("fn", "");
		if(id>=100 && !StringUtils.equals(f_ident, bean.getIdent())){
			ctx.not_found();
			return ;
		}
		
		FileInputStream fis = null;
		try{
			java.io.File file = StorageService.FILES.readFile(bean.getPath());
			fis = new FileInputStream(file);
			//设置 content-type
			ctx.response().setContentLength((int)file.length());
			String ext = FilenameUtils.getExtension(bean.getPath());
			String mine_type = Multimedia.mime_types.get(ext);
			if(mine_type != null)
				ctx.response().setContentType(mine_type);
			String ua = ctx.header("user-agent");
			if(ua != null && ua.indexOf("Firefox")>=0)
				ctx.header("Content-Disposition","attachment; filename*=\"utf8''" + 
				  LinkTool.encode_url(bean.getName())+"."+ext+"\"");
			else
				ctx.header("Content-Disposition","attachment; filename=" + 
				  LinkTool.encode_url(bean.getName()+"."+ext));
			IOUtils.copy(fis, ctx.response().getOutputStream());
		//}catch(FileNotFoundException e){
		//	ctx.not_found();
		}finally{
			IOUtils.closeQuietly(fis);
		}
	}
	
	/**
	 * 文件上传,表单需要以下参数：name,file
	 * @param ctx
	 * @throws IOException 
	 */
	@Annotation.UserRoleRequired(role=User.ROLE_EDITOR)
	public void upload(RequestContext ctx) throws IOException {
		File form = ctx.form(File.class);
		if(StringUtils.isBlank(form.getName()))
			throw ctx.error("file_name_empty");
		java.io.File file = ctx.file("file");
		if(file == null)
			throw ctx.error("file_empty");
		if(!File.IsLegalFile(file.getName()))
			throw ctx.error("file_illegal");
		String the_path = form.getUrl();
		//判断文件是否存在
		if(StringUtils.isNotBlank(the_path) && StorageService.FILES.exist(the_path))
			throw ctx.error("file_exists", the_path);
		String uri = StringUtils.isBlank(the_path)?
		StorageService.FILES.save(file):StorageService.FILES.save(file,the_path);//文件存储
		form.setSize(file.length());
		form.setUrl(uri);
		form.setPath(uri);
		form.setUser(ctx.user().getId());
		form.Save();
		throw ctx.error("file_upload_ok", 
		  LinkTool.action("file/download?id="+form.getId()+"&fn="+form.getIdent()));
	}

	/**
	 * 文件修改
	 * @param ctx
	 * @throws IOException 
	 */
	@Annotation.UserRoleRequired(role=User.ROLE_EDITOR)
	public void edit(RequestContext ctx) throws IOException {
		File form = ctx.form(File.class);
		if(StringUtils.isBlank(form.getName()))
			throw ctx.error("file_name_empty");
		File bean = File.INSTANCE.Get(form.getId());
		java.io.File pic = ctx.file("file");
		if(pic != null){
			if(!File.IsLegalFile(pic.getName()))
				throw ctx.error("file_illegal");
			bean.setSize(pic.length());
			StorageService.FILES.save(pic, bean.getPath());//文件存储
		}
		bean.setName(form.getName());
		bean.setDetail(form.getDetail());
		bean.setOptions(form.getOptions());
		bean.Update();
		throw ctx.error("file_edit_ok");
	}

	/**
	 * 文件删除
	 * @param ctx
	 * @throws IOExceptionn  
	 */
	@Annotation.UserRoleRequired(role=User.ROLE_ADMIN)
	public void delete(RequestContext ctx) throws IOException {
		long id = ctx.id();
		File file = File.INSTANCE.Get(id);
		if(file != null){
			StorageService.FILES.delete(file.getPath());
			file.Delete();			
		}
	}

}
